package com.device.search.jframe;

import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.table.DefaultTableCellRenderer;
import javax.swing.table.DefaultTableModel;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import com.device.search.model.Device;
import com.device.search.service.ConstructionDeviceService;
import com.device.search.utils.SpringUtil;

import javax.swing.JButton;
import java.awt.event.ActionListener;
import java.util.List;
import java.awt.event.ActionEvent;
import javax.swing.JLabel;
import javax.swing.JTextField;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import java.awt.Font;

@Component
public class DeviceDetailsJFrame extends JFrame implements ActionListener {

	@Autowired
	private ConstructionDeviceService deviceService;

	private static final long serialVersionUID = 1L;
	private JPanel contentPane;
	private JTextField projectName;
	private JTextField deviceNo;
	
	public static final String[] TABLE_HEAD = { "项目ID", "设备ID", "项目名称", "设备编号" };
	public Object[][] tableBody = new Object[20][4];
	private JTable table;

	/**
	 * Create the frame.
	 */
	public DeviceDetailsJFrame() {
		setTitle("设备查询");
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 1050, 650);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		DefaultTableModel tableModel = new DefaultTableModel(tableBody, TABLE_HEAD);
		
		JButton btnNewButton = new JButton("返回首页");
		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceJFrame frame = SpringUtil.getBean(DeviceJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		btnNewButton.setBounds(835, 541, 113, 27);
		contentPane.add(btnNewButton);

		JButton btnNewButton_1 = new JButton("设备状态查询");
		btnNewButton_1.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceStatusJFrame frame = SpringUtil.getBean(DeviceStatusJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		btnNewButton_1.setBounds(670, 541, 142, 27);
		contentPane.add(btnNewButton_1);

		JButton btnNewButton_2 = new JButton("设备数据查询");
		btnNewButton_2.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				DeviceDataJFrame frame = SpringUtil.getBean(DeviceDataJFrame.class);
				frame.setVisible(true);
				dispose();
			}
		});
		btnNewButton_2.setBounds(503, 541, 142, 27);
		contentPane.add(btnNewButton_2);

		JLabel label = new JLabel("设备编号：");
		label.setBounds(429, 32, 98, 18);
		contentPane.add(label);

		JLabel lblNewLabel = new JLabel("项目名称：");
		lblNewLabel.setBounds(44, 33, 98, 18);
		contentPane.add(lblNewLabel);

		JButton button = new JButton("查询");
		button.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				List<Device> deviceList = deviceService.findByProjectNameAndDeviceNo(projectName.getText(),
						deviceNo.getText());
				tableBody = new Object[deviceList.size()][4];
				for (int i = 0; i < deviceList.size(); i++) {
					tableBody[i][0] = deviceList.get(i).getConstruction_id();
					tableBody[i][1] = deviceList.get(i).getId();
					tableBody[i][2] = deviceList.get(i).getProject_name();
					tableBody[i][3] = deviceList.get(i).getDvice_no();
				}
				tableModel.setDataVector(tableBody, TABLE_HEAD);
			}
		});
		button.setBounds(835, 29, 113, 27);
		contentPane.add(button);

		projectName = new JTextField();
		projectName.setText("");
		projectName.setBounds(128, 30, 274, 24);
		contentPane.add(projectName);
		projectName.setColumns(10);

		deviceNo = new JTextField();
		deviceNo.setText("HC000");
		deviceNo.setBounds(509, 29, 274, 24);
		contentPane.add(deviceNo);
		deviceNo.setColumns(10);
		
		JScrollPane scrollPane = new JScrollPane();
		scrollPane.setBounds(41, 91, 937, 413);
		contentPane.add(scrollPane);
		
		table = new JTable();
		table.setFont(new Font("微软雅黑", Font.PLAIN, 20));
		table.setRowHeight(30);                    //设置行高度
		table.setModel(tableModel);                //设置内容
		DefaultTableCellRenderer r = new DefaultTableCellRenderer();    
		r.setHorizontalAlignment(JLabel.CENTER);   
		table.setDefaultRenderer(Object.class,r);
		scrollPane.setColumnHeaderView(table);
		scrollPane.setViewportView(table);
		
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		// TODO Auto-generated method stub
	}
}
